//Infinity
package org.rsbot.script.randoms.antiban;

import java.awt.Rectangle;

import org.rsbot.bot.Bot;
import org.rsbot.gui.AccountManager;
import org.rsbot.script.Antiban;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSInterfaceChild;

/*
 * Cleaned and Updated By Secret Spy
 * Updated On 02/24/11 By Somanayr
 */
@ScriptManifest(authors = {"Secret Spy, Somanayr"}, name = "LoginBot", version = 1.4)
public class LoginBot extends Antiban {
	
	private static class Interfaces {
		public static final int Main = 906;
	    public static final int MainChild = 171;
	    public static final int MainChildComponentID = 4;
	    public static final int LoginScreen = 596;
	    public static final int Username = 65;//
	    public static final int UsernameWindow = 37;//
	    public static final int Password = 71;//
	    public static final int PasswordWindow = 39;//
	    public static final int ButtonLogin = 42;//
	    public static final int TextReturn = 11;
	    public static final int ButtonBack = 55;
	    public static final int WelcomeScreen = 906;
	    public static final int WelcomeScreenButtonPlay1 = 171;
	    public static final int WelcomeScreenButtonPlay2 = 172;
	    public static final int TryAgainButton1 = 57;
	    public static final int TryAgainButton2 = 59;
	    public static final int GraphicNotice = 976;
	    public static final int GraphicLeaveAlone = 6;
	    
	}
	
	private static final int IndexLoggedOut = 3;
    private static final int IndexLobby = 7;

    private int InvalidCount, WorldFullCount;

    @Override
    public boolean activateCondition() {
        final int idx = getLoginIndex();
        if (idx == IndexLoggedOut || idx == IndexLobby)
            return true;
        return false;
    }

    public String getUsername() {
        return Bot.getAccountName().toLowerCase().trim();
    }

    @Override
    public int loop() {
        final String username = getUsername();
        final String returnText = ifaces.getInterface(Interfaces.LoginScreen).getChild(Interfaces.TextReturn).getText().toLowerCase();
        int textlength;
        if (getLoginIndex() != IndexLoggedOut) {
            if (!isWelcomeScreen()) {
                wait(random(1000, 2000));
            }
            if (getLoginIndex() == IndexLobby) {
                final RSInterface WelcomeScreen = getInterface(Interfaces.WelcomeScreen);
                final RSInterfaceChild WelcomeScreenButtonPlay1 = WelcomeScreen.getChild(Interfaces.WelcomeScreenButtonPlay1);
                final RSInterfaceChild WelcomeScreenButtonPlay2 = WelcomeScreen.getChild(Interfaces.WelcomeScreenButtonPlay2);
                clickMouse(WelcomeScreenButtonPlay1.getAbsoluteX(),
                        WelcomeScreenButtonPlay1.getAbsoluteY(),
                        WelcomeScreenButtonPlay2.getAbsoluteX() + WelcomeScreenButtonPlay2.getWidth() - WelcomeScreenButtonPlay1.getAbsoluteX(),
                        WelcomeScreenButtonPlay1.getHeight(), true);

                for (int i = 0; i < 4 && getLoginIndex() == IndexLobby; i++) {
                    wait(500);
                }
                if (returnText.contains("update")) { // unsure of right interface at this time
                    log("It Seems Runescape Has Updated, Please Reload Infinity.");
                    stopScript(false);
                    return -1;
                }

                if (ifaces.getInterface(Interfaces.Main).getChild(221).getText().toLowerCase().contains("move") || ifaces.getInterface(Interfaces.Main).getChild(221).getText().toLowerCase().contains("need")) {
                    log("Attempted To Log Into A Members World As A Free To Play Player");
                    log("Changing to a F2P World");
                    if (ifaces.getInterface(Interfaces.Main).getChild(228).isValid()) {
                        atInterface(Interfaces.Main, 228);
                        wait(random(1000, 1500));
                    }
                    if (ifaces.getInterface(Interfaces.WelcomeScreen).getChild(174).isValid()) {
                        atInterface(Interfaces.WelcomeScreen, 174);
                        wait(random(1000, 1500));
                        moveMouse(572, 189);
                        wait(random(1000, 1500));
                        clickMouse(true);
                        wait(random(1000, 1500));
                        moveMouse(random(223, 520), random(209, 428)); //// Moves Mouse Randomly Within the World(s)
                        wait(random(1000, 1500));
                        clickMouse(true);
                        wait(random(1000, 1500));
                        atInterface(Interfaces.WelcomeScreen, Interfaces.WelcomeScreenButtonPlay1);
                        wait(random(1000, 1500));
                    }
                }
            }
            return -1;
        }
        if (getInterface(Interfaces.LoginScreen).isValid()) {
            if (returnText.contains("disable")) {
                log("Ohh Noo It Seems Your Account Is Banned/Disabled.");
                stopScript(false);
                return -1;
            }
            if (returnText.contains("incorrect")) {
                log("failed to login five times in a row.");
                log("Stopping Script.");
                stopScript(false);
                return -1;
            }
            if (returnText.contains("invalid")) {
                if (InvalidCount > 7) {
                    log("I Am Unable To Login, After 8 Attempts.");
                    log("Please Verify That Your Account Username/Password Is Correct.");
                    stopScript(false);
                }

                log("Failed " + InvalidCount + 1 + " times to log in out of 8 tries");
                final RSInterfaceChild TryAgainButton1 = getInterface(Interfaces.LoginScreen).getChild(Interfaces.TryAgainButton1);
                final RSInterfaceChild TryAgainButton2 = getInterface(Interfaces.LoginScreen).getChild(Interfaces.TryAgainButton2);
                clickMouse(TryAgainButton1.getAbsoluteX(),
                        TryAgainButton1.getAbsoluteY(),
                        TryAgainButton2.getAbsoluteX() + TryAgainButton2.getWidth() - TryAgainButton1.getAbsoluteX(),
                        TryAgainButton1.getHeight(), true);
                InvalidCount++;
                return (random(500, 2000));
            }
            if (returnText.contains("full")) {
                if (WorldFullCount > 30) {
                    log("World Is Full. Waiting for about 12 seconds.");
                    wait(random(10000, 15000));
                    WorldFullCount = 0;
                }
                wait(random(1000, 1200));
                WorldFullCount++;
            }
            if (returnText.contains("world"))
                return random(1000, 1200);
            if (returnText.contains("performing login"))
                return random(1000, 1200);
        }
        if (getLoginIndex() == IndexLoggedOut) {
            if (getInterface(Interfaces.GraphicNotice).getChild(Interfaces.GraphicLeaveAlone).isValid()) {
                atInterface(ifaces.getInterface(Interfaces.GraphicNotice).getChild(Interfaces.GraphicLeaveAlone));
                return random(500, 600);
            }

            if (!atLoginScreen()) {
                atInterface(ifaces.getInterface(Interfaces.Main).getChild(Interfaces.MainChild));
                return random(500, 600);
            }
            if (isUsernameFilled() && isPasswordFilled()) {
                atInterface(ifaces.getInterface(Interfaces.LoginScreen).getChild(Interfaces.ButtonLogin));
                return random(500, 600);
            }
            if (!isUsernameFilled()) {
                atLoginInterface(ifaces.getInterface(Interfaces.LoginScreen).getChild(Interfaces.UsernameWindow));
                wait(random(500, 700));
                textlength = ifaces.getInterface(Interfaces.LoginScreen).getChild(Interfaces.Username).getText().length() + random(3, 5);
                for (int i = 0; i <= textlength + random(1, 5); i++) {
                    sendText("\b", false);
                    if (random(0, 2) == 1) {
                        wait(random(25, 100));
                    }
                }
                sendText(username, false);
            } else if (!isPasswordFilled()) {
                atLoginInterface(ifaces.getInterface(Interfaces.LoginScreen).getChild(Interfaces.PasswordWindow));
                wait(random(500, 700));
                textlength = ifaces.getInterface(Interfaces.LoginScreen).getChild(Interfaces.Password).getText().length() + random(3, 5);
                for (int i = 0; i <= textlength + random(1, 5); i++) {
                    sendText("\b", false);
                    if (random(0, 2) == 1) {
                        wait(random(25, 100));
                    }
                }
                sendText(AccountManager.getPassword(Bot.getAccountName()), false);
            }
        }
        if (isActive())
            return -1;
        return random(500, 2000);
    }

    /*Clicks past all of the letters*/
    private boolean atLoginInterface(final RSInterfaceChild i) {
        if (!i.isValid())
            return false;
        final Rectangle pos = i.getArea();
        if (pos.x == -1 || pos.y == -1 || pos.width == -1 || pos.height == -1)
            return false;
        final int dy = (int) (pos.getHeight() - 4) / 2;
        final int maxRandomX = (int) (pos.getMaxX() - pos.getCenterX());
        final int midx = (int) (pos.getCenterX());
        final int midy = (int) (pos.getMinY() + pos.getHeight() / 2);
        if (i.getIndex() == Interfaces.PasswordWindow) {
            clickMouse(MinX(i), midy + random(-dy, dy), true);
        } else {
            clickMouse(midx + random(1, maxRandomX), midy + random(-dy, dy), true);
        }
        return true;
    }

    /*
     * Returns x int based on the letters in a Child
     * Only the password text is needed as the username text cannot reach past the middle of the interface
     */
    private int MinX(final RSInterfaceChild a) {
        int x = 0;
        final Rectangle pos = a.getArea();
        final int dx = (int) (pos.getWidth() - 4) / 2;
        final int midx = (int) (pos.getMinX() + pos.getWidth() / 2);
        if (pos.x == -1 || pos.y == -1 || pos.width == -1 || pos.height == -1)
            return 0;
        for (int i = 0; i < ifaces.getInterface(Interfaces.LoginScreen).getChild(Interfaces.Password).getText().length(); i++) {
            x += 11;
        }
        if (x > 44)
            return (int) (pos.getMinX() + x + 15);
        else
            return midx + random(-dx, dx);
    }

    private boolean atLoginScreen() {
        return getInterface(Interfaces.LoginScreen).isValid();
    }

    private boolean isUsernameFilled() {
        final String username = getUsername();
        return ifaces.getInterface(Interfaces.LoginScreen).getChild(Interfaces.Username).getText().toLowerCase().equalsIgnoreCase(username);
    }

    private boolean isPasswordFilled() {
        return ifaces.getInterface(Interfaces.LoginScreen).getChild(Interfaces.Password).getText().toLowerCase().length() == AccountManager.getPassword(Bot.getAccountName()).length();
    }
}
